using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Comteck.Dto.Orders;
using Comteck.Ktt.Services.Orders;
using Comteck.Localization;
using Comteck.Winforms.Data;
using Comteck.Winforms.Forms;
using Comteck.Winforms.Infrastructure;
using DevExpress.XtraBars;

namespace Comteck.Ktt.WinUI.Orders {
  /// <summary>
  /// 自提点标签批量维护
  /// </summary>
  public partial class FrmSelfOrgBatchAction : FrmBase {
    private readonly ISelfOrgService selfOrgService;

    /// <summary>
    /// 
    /// </summary>
    [Browsable(false)]
    public List<int> IdList { get; set; }

    public FrmSelfOrgBatchAction(ISelfOrgService selfOrgService) {
      this.selfOrgService = selfOrgService;
      InitializeComponent();
    }

    private async void FrmSelfOrgBatchAction_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

      var selfOrgTagDtoList = await ServerDbDataHelper.GetAllSelfOrgTagAsync();
      await BindHelper.BindSelfOrgTag(rilpTagIds, null, selfOrgTagDtoList);
      await BindHelper.BindSelfOrgTag(batchTagIds, null, selfOrgTagDtoList);
      await this.LoadFormData();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private async Task LoadFormData() {
      ajaxLoad.Show();
      var result = await selfOrgService.GetListByIdsAsync(this.IdList);
      gcMain.DataSource = result;
      ajaxLoad.Hide();
    }

    private void btnTagId_Click(object sender, EventArgs e) {
      this.SetBatchValues(nameof(SelfOrgDto.TagIds), batchTagIds.EditValue.To<string>());
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="column"></param>
    private void SetBatchValues(string column, dynamic value) {
      var list = gvMain.GetSelectedRowsList<SelfOrgDto>();
      if (list.IsNullOrEmpty()) {
        return;
      }

      switch (column) {
        case nameof(SelfOrgDto.TagIds):
          list.ForEach(x => x.TagIds = value);
          break;
        default:
          break;
      }
      gvMain.RefreshData();
      gvMain.CloseEditor();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnSave_ItemClick(object sender, ItemClickEventArgs e) {
      gvMain.CloseEditor();

      // 选中的
      var rows = gcMain.DataSource as IEnumerable<SelfOrgDto>;
      if (rows.IsNullOrEmpty()) {
        return;
      }

      var dialog = this.ShowMsg(BaseResources.Save_Confirm, null, MessageBoxButtons.YesNo,
        MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

      if (dialog == DialogResult.No) {
        return;
      }

      ajaxLoad.Show();
      try {
        await selfOrgService.SaveBatchAsync(rows);
        ajaxLoad.Hide();
        this.ShowMsg(BaseResources.Save_Success);
        this.DialogResult = DialogResult.Yes;
      } catch (Exception ex) {
        ajaxLoad.Hide();
        this.ShowMsg(BaseResources.Save_Failed_Reason, ex.Message);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmSelfOrgBatchAction_FormClosing(object sender, FormClosingEventArgs e) {
      var list = gcMain.DataSource as IEnumerable<SelfOrgDto>;
      if (list.Any(x => x.TagIds.IsNullOrEmpty())) {
        var dialog = this.ShowMsg("当前还有自提点没有维护标签，您确定要关闭吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

        if (dialog == DialogResult.No) {
          e.Cancel = true;
        } else {
          e.Cancel = false;
        }
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }
  }
}
